<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            padding-top: 50px;
        }
        .upload-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }
        .upload-button {
            margin-top: 20px;
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        .upload-button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="upload-container">
    <h2>文件上传</h2>
    <input type="file" id="fileInput" name="file" accept=".txt, .pdf, .docx, .jpg, .png">
    <button onclick="uploadFile()">上传</button>
    <p id="uploadStatus"></p>
</div>

<script>
    // function uploadFile() {
    //     const fileInput = document.getElementById('fileInput');
    //     const file = fileInput.files[0];
    //     const formData = new FormData();
    //     formData.append('file', file);
    //
    //     const uploadStatus = document.getElementById('uploadStatus');
    //     uploadStatus.textContent = '正在上传...';
    //
    //     const xhr = new XMLHttpRequest();
    //     xhr.open('POST', 'http://localhost:8080/fileInfo/upload', true); // 这里的 URL 应与后端处理文件上传的端点匹配
    //
    //     xhr.onload = function () {
    //         if (this.status === 200) {
    //             uploadStatus.textContent = '上传成功';
    //         } else {
    //             uploadStatus.textContent = '上传失败';
    //         }
    //     };
    //
    //     xhr.onerror = function () {
    //         uploadStatus.textContent = '上传过程中发生错误';
    //     };
    //
    //     xhr.send(formData);
    // }
    function uploadFile() {
        const fileInput = document.getElementById('fileInput');
        const file = fileInput.files[0];
        const formData = new FormData();
        formData.append('file', file);

        const uploadStatus = document.getElementById('uploadStatus');
        uploadStatus.textContent = '正在上传...';

        fetch('http://localhost:8080/fileInfo/upload', {
            method: 'POST',
            body: formData
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok.');
                }
                return response.text();
            })
            .then(data => {
                uploadStatus.textContent = '上传成功';
                // 处理返回的数据（如果需要）
            })
            .catch(error => {
                uploadStatus.textContent = '上传失败';
                console.error('Error:', error);
            });
    }
</script>
</body>
</html>